package com.buybuyyi.stylistcrm.resource.controllers.goods;

import com.alibaba.fastjson.JSONArray;
import com.buybuyyi.stylistcrm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import stylistcrm.core.query.ResultPagerModel;
import stylistcrm.core.services.goods.dto.*;
import stylistcrm.core.services.goods.service.IGoodsService;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.user.EnumUserType;
import stylistcrm.core.services.user.User;
import stylistcrm.core.services.user.service.UserService;

import java.util.List;

/**
 * Created by yanwe on 2016/8/10.
 */

@Controller
@RequestMapping("/goods")
public class GoodsController {
	
	
	@Autowired
	IGoodsService goodsService;
	
	@Autowired
	UserService userService;
	
    @ResponseBody
    @RequestMapping("/test")
    public String home(){
        return "hello word";
    }
    
    @ResponseBody
    @RequestMapping("/saveGoods")
    public GoodsResultDTO saveGoods(GoodsDTO goodsDto){
    	GoodsResultDTO resultDto=new GoodsResultDTO();

		if(goodsDto != null){
			setGoodGrand(goodsDto);
			setdicTagsDto(goodsDto);
			setGoodsClassFication(goodsDto);
			setdicYargeDto(goodsDto);
		}
    	try {
    		User user=userService.getCurrentUser();
    		if(user == null){
    			resultDto.setCode("0");
    			resultDto.setMessage("用户不存在");
    			return resultDto;
    		}
    		goodsDto.setUserId(3L);
    		resultDto=goodsService.saveGoods(goodsDto);
		} catch (Exception e) {
			resultDto.setCode("0");
			resultDto.setMessage("系统异常");
			return resultDto;
		}
    	return resultDto;
    }

	/**
	 * 将字符串转换为对象
	 * @param
	 */
	private void setGoodGrand(GoodsDTO goodsDto){
		if(null==goodsDto){
			return;
		}
		String brandStr=goodsDto.getDicBrandDtoNames();
		if(StringUtil.isBlank(brandStr)){
			return;
		}
		List<DicBrandDTO> goodsBrand= JSONArray.parseArray(brandStr,DicBrandDTO.class);
		if(null!=goodsBrand){
			goodsDto.setDicBrandDto(goodsBrand);
		}
	}


	/**
	 * 将字符串转换为对象
	 * @param
	 */
	private void setdicTagsDto(GoodsDTO goodsDto){
		if(null==goodsDto){
			return;
		}
		String dicTagesStr=goodsDto.getDicTagsDtoNames();
		if(StringUtil.isBlank(dicTagesStr)){
			return;
		}
		List<DicTagsDTO> goodsTages= JSONArray.parseArray(dicTagesStr,DicTagsDTO.class);
		if(null!=goodsTages){
			goodsDto.setDicTagsDto(goodsTages);
		}
	}


	/**
	 * 将字符串转换为对象
	 * @param
	 */
	private void setdicYargeDto(GoodsDTO goodsDto){
		if(null==goodsDto){
			return;
		}
		String yardages=goodsDto.getGoodYadageStrings();
		if(StringUtil.isBlank(yardages)){
			return;
		}
		List<GoodsColorDto> goodsYardageTages= JSONArray.parseArray(yardages,GoodsColorDto.class);
		if(null!=goodsYardageTages){
			goodsDto.setGoodsYadageDtos(goodsYardageTages);
		}
	}


	/**
	 * 将字符串转换为对象
	 * @param
	 */
	private void setGoodsClassFication(GoodsDTO goodsDto){
		if(null==goodsDto){
			return;
		}
		String classDiction=goodsDto.getDicClassificationsDtoNames();
		if(StringUtil.isBlank(classDiction)){
			return;
		}
		List<DicClassificationsDTO> goodsClassFication= JSONArray.parseArray(classDiction,DicClassificationsDTO.class);
		if(null!=goodsClassFication){
			goodsDto.setDicClassificationsDto(goodsClassFication);
		}
	}







	/**
	 * 将字符串转换为对象
	 * @param
	 */
	private void setGrandGoodsList(GoodsDTO goodsDto){
		if(null==goodsDto){
			return;
		}
		String brandStr=goodsDto.getDicBrandDtoNames();
		if(StringUtil.isBlank(brandStr)){
			return;
		}
		List<DicBrandDTO> goodsBrand= JSONArray.parseArray(brandStr,DicBrandDTO.class);
		if(null!=goodsBrand){
			goodsDto.setDicBrandDto(goodsBrand);
		}
	}

    public static void main(String args[]){

	}
    
    
    @ResponseBody
    @RequestMapping("/getGoodsList")
    public ResultPagerModel<GoodsDTO> getGoodsList(GoodsPageDTO goodsPageDto){
    	
    	ResultPagerModel<GoodsDTO> pageModel=new ResultPagerModel<GoodsDTO>();
    	try {
    		User user=userService.getCurrentUser();
    		//用户类型是设计师···需要查询自己的商品
    		if(user.isUserType(EnumUserType.DESIGNER)){
    			goodsPageDto.setUserId(user.getMemberId());
    		}
    		pageModel=goodsService.getGoodsByContation(goodsPageDto);
    		
		} catch (Exception e) {
		    
		}
    	return pageModel;
    }
    
    @ResponseBody
    @RequestMapping("/changeYadage")
    public GoodsResultDTO changeGoodsYadage(GoodsYardageChangeDto yardageDao){
    	GoodsResultDTO  resultDto=new GoodsResultDTO();
		User user= null;
		try {
			user = userService.getCurrentUser();
		} catch (BizException e) {
			e.printStackTrace();
		}
		yardageDao.setUserId(user.getMemberId());
    	try {
    		resultDto=goodsService.changeGoodsStatus(yardageDao);
    		return resultDto;
		} catch (Exception e) {
			e.printStackTrace();
			resultDto.setCode("0");
			resultDto.setMessage("系统异常");
		}
    	return resultDto;
    }
    
}
